<?php
sdk_import('&.php_include.Log');
class SDK_Log
{
    protected $token;
    protected $logger;
    
    public function __construct($name)
    {
        $this->token = $name;
        $type = "LOG_SDK";
        if($name=="OASApi" || $name=="OAApi") {
        	$type = "LOG_SDK_OASOA";
        }        		
  
//        $this->logger = new OaLog($type, "", array("lineFormat"=>'%1$s%2$s	%3$s	%4$s', 'timeFormat'=>"%Y-%m-%d %H:%M:%S"));

        $logger = Log::factory('composite');
        $conf = array(
            'lineFormat' => '%1$s%2$s	%3$s	%4$s',
            'timeFormat' => '%Y-%m-%d %H:%M:%S',
            'mode' => '0666'
        );
        $loggerDisplay = Log::factory('display', '', '', $conf, PEAR_LOG_ERR);
        $dir = "/var/log/sp/$type";
        if (!is_dir($dir)) {
            exec("mkdir -p $dir");
        }
        $filename = $type . '_' . date('Ymd') . '.log';
        $loggerFile = Log::factory('file', "$dir/$filename", '', $conf, PEAR_LOG_INFO);
        if (__OA_ENV == 'DEV') {
            $logger->addChild($loggerDisplay);
            $logger->addChild($loggerFile);
        } else if (__OA_ENV == 'TEST') {
            $logger->addChild($loggerDisplay);
            $logger->addChild($loggerFile);
        } else {
            $logger->addChild($loggerFile);
        }
        $this->logger = $logger;
    }
    
    public function log ($log)
    {
        return $this->debug($log);
    }
    
    public static function getLogger($name)
    {
        return new SDK_Log($name);
    }
    
    public static function &getLogInstance ($name)
    {
        try {
            //$configs = parse_ini_file(SDK_CONFIGS_PATH . 'log4php.conf.php');
            $logger = &self::getLogger($name);
            return $logger;
        } catch (Exception $e) {
            return FALSE;
        }
        return FALSE;
    }
    
    public function info($msg)
    {
        $msg = $this->token. "\t". $msg;
        $this->logger->info($msg);
    }
    
    public function warn($msg)
    {
        $msg = $this->token. "\t". $msg;
        $this->logger->notice($msg);
    }
    
    public function error($msg)
    {
        $msg = $this->token. "\t". $msg;
        $this->logger->warning($msg);
    }
    
    public function fatal($msg)
    {
        $msg = $this->token. "\t". $msg;
        $this->logger->err($msg);
    }
    
} 